package com.springboot.demo.service.impl;

import com.springboot.demo.entity.CentralMonitor;
import com.springboot.demo.exception.BusinessException;
import com.springboot.demo.mapper.CentralMonitorMap;
import com.springboot.demo.service.CentralMonitorService;
import com.springboot.demo.utils.Constant;
import com.springboot.demo.utils.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wubc683
 * @desc
 * @date 2021-11-26 11:57
 */
@Service
@Slf4j
public class CentralMonitorServiceImpl implements CentralMonitorService {
    @Autowired
    CentralMonitorMap centralMonitorMap;
    private static final Integer CENTRAL_MONITOR_MAX_COUNT = 10;

    @Override
    public List<CentralMonitor> getAllCentralMonitor(Long uid) {
        return centralMonitorMap.getAllCentralMonitor(uid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCentralMonitor(Long uid, String name) {
        // 校验集中监控分组数量是否达到上限
        List<CentralMonitor> allCentralMonitor = centralMonitorMap.getAllCentralMonitor(uid);
        if (allCentralMonitor.size() >= CENTRAL_MONITOR_MAX_COUNT) {
            throw new BusinessException(ResponseResult.CENTRAL_MONITOR_COUNT_UPPER_LIMIT);
        }
        // 校验分组名称是否重复
        for (int i = 0; i < allCentralMonitor.size(); i++) {
            if (allCentralMonitor.get(i).getName().equals(name)) {
                throw new BusinessException(ResponseResult.CENTRAL_MONITOR_NAME_DUPLICATE);
            }
        }
        centralMonitorMap.saveCentralMonitor(uid,name);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCentralMonitor(Long uid, Long cid, String name) {
        List<CentralMonitor> allCentralMonitor = centralMonitorMap.getAllCentralMonitor(uid);
        boolean findCentralMonitorId = false;
        // 校验分组名称是否重复
        for (int i = 0; i < allCentralMonitor.size(); i++) {
            if (allCentralMonitor.get(i).getName().equals(name)
            && !allCentralMonitor.get(i).getId().equals(cid)) {
                throw new BusinessException(ResponseResult.CENTRAL_MONITOR_NAME_DUPLICATE);
            }
            if(allCentralMonitor.get(i).getId().equals(cid)){
                findCentralMonitorId = true;
            }
        }
        // 校验集中监控分组id是否属于用户
        if(!findCentralMonitorId){
            throw new BusinessException(ResponseResult.USER_DO_NOT_HAVE_THIS_CENTRAL_MONITOR);
        }
        centralMonitorMap.updateCentralMonitor(cid,name);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCentralMonitor(Long uid, Long cid) {
        // 校验集中监控分组id是否属于用户
        CentralMonitor centralMonitorById = centralMonitorMap.getCentralMonitorById(cid);
        if(centralMonitorById == null || !centralMonitorById.getOwnerId().equals(uid)){
            throw new BusinessException(ResponseResult.USER_DO_NOT_HAVE_THIS_CENTRAL_MONITOR);
        }
        centralMonitorMap.deleteCentralMonitor(cid);
    }
}
